package models;

import javax.persistence.Lob;

import play.Logger;
import play.data.validation.MaxSize;
import play.data.validation.Required;
import siena.*;
import siena.core.lifecycle.PreInsert;
import siena.core.lifecycle.PreSave;
import siena.core.lifecycle.PreUpdate;
import util.ModelUtil;

import com.google.appengine.api.datastore.Blob;

/**
 * @author oleksandr.bezhan
 * 
 *         Apr 7, 2012
 */
public class Recipe extends Model {
    public static Query<Recipe> all() {
        return Model.all(Recipe.class);
    }

    public static Recipe findByName(String name) {
        return Recipe.all().filter("name", name).get();
    }

    public String author;

    public String code;

    @Filter("recipe")
    public Query<RecipeComment> comments;

    @MaxSize(3000)
    public String description;

    public boolean display;

    @Id(Generator.AUTO_INCREMENT)
    public Long id;

    public Blob image2;
    
    public Blob image;

    @Filter("recipe")
    public Query<Ingredient> ingredients;

    @Required
    public String name;

    public Blob thumb2;
    
    public Blob thumb;

    public Recipe() {
        this.display = true;
    }

    public Recipe(Long id) {
        this.id = id;
    }

    @SuppressWarnings("unused")
    @PreInsert
    @PreUpdate
    @PreSave
    private void generateCode() {
        if ((this.code == null) || this.code.equals("")) {
            this.code = ModelUtil.translit(this.name);
            Logger.debug("Generated code: " + this.code);
        }
    }

    public void setName(String name) {
        this.name = name;
        this.code = ModelUtil.translit(name);
    }

    @Override
    public String toString() {
        return this.name;
    }
}